﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

namespace XDesigner.ORM
{
    [Serializable()]
    public class QueryCondition
    {
        private static volatile int InstanceCounter = 0;

        private string _Name = "Condition" + ( InstanceCounter ++ ); 

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _FieldName = null;

        public string FieldName
        {
            get { return _FieldName; }
            set { _FieldName = value; }
        }

        private string _Method = "=";

        public string Method
        {
            get { return _Method; }
            set { _Method = value; }
        }

        private object _Value = null;

        public object Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        public override string ToString()
        {
            return "Condition:" + this.FieldName + " " + this.Method + " " + this.Value;
        }
    }

    [Serializable()]
    [System.Diagnostics.DebuggerDisplay("QueryConditionList Count={ Count }")]
    [System.Diagnostics.DebuggerTypeProxy(typeof(ORMDebugView))]
    public class QueryConditionList : System.Collections.CollectionBase
    {
        public QueryConditionList()
        {
        }

        private string _ContactOperator = "And";

        public string ContactOperator
        {
            get { return _ContactOperator; }
            set { _ContactOperator = value; }
        }

        public QueryCondition this[int index]
        {
            get
            {
                return (QueryCondition)this.List[index];
            }
        }

        public QueryCondition this[string name]
        {
            get
            {
                foreach (QueryCondition item in this)
                {
                    if (string.Compare(item.Name, name, true) == 0)
                    {
                        return item;
                    }
                }
                return null;
            }
        }

        public int Add(QueryCondition item)
        {
            return this.List.Add(item);
        }

        public QueryCondition AddCondition(
            string fieldName,
            string method, 
            object Value)
        {
            QueryCondition item = new QueryCondition();
            item.FieldName = fieldName;
            item.Method = method;
            item.Value = Value;
            this.List.Add(item);
            return item;
        }

        public void Remove(QueryCondition item)
        {
            this.List.Remove(item);
        }
    }

    //public enum CompareMethod
    //{
    //    Equal ,
    //    MoreThan,
    //    LessThan,
    //    Include,
    //    Exclude
    //}
}
